<?php
/****************************************
*mysql db class
****************************************/
class Mysql 
{
        public static function getInstance()
        {
        	if( !isset( self::$dbConnection ) ){
        		self::$dbConnection = new Mysql();
        	}
        	return self::$dbConnection;
        }
        
    	private  function __construct()
    	{
    		$cfg = $GLOBALS['cfg'];
    		$this->dbHost = $cfg['db']['host'];
    		$this->dbUser = $cfg['db']['username'];
    		$this->dbpass = $cfg['db']['password'];
    		$this->dbName = $cfg['db']['datebase'];
            @$this->dbConnection = mysql_connect($this->dbHost,$this->dbUser,$this->dbpass);
            @mysql_select_db($this->dbName);
            @$this->_query("SET NAMES '" . $this->dbCharset . "'");
    	}
    	
    	public function __destruct()
    	{
    		$this->disConnect();
    	}
           
    	private function __clone(){
    		trigger_error('Only allow one instance!',E_USER_ERROR);
    	}
    	
        public function disConnect() 
        {
       		if (!$this->dbConnection){
        		mysql_close($this->dbConnection);
            }
            $this->dbConnection = null;                                                
        }


	    public function getOne( $sql ) 
	    {
	    	$res = $this->_query( $sql );
			@$row = mysql_fetch_row ( $res );
			@mysql_free_result ( $res );
			return @ isset ( $row[0] ) ? $row[0] : null;                    
	    }
	
	    public function getRow( $sql ) 
	    {
	    	$res = $this->_query( $sql );
			@$row = mysql_fetch_assoc( $res );
			@mysql_free_result( $res );                    
	        return @$row;
	    }
	
	    public function getCol( $sql ) 
	    {
			$res = $this->_query( $sql );
			$data = array ();
			while ( @$row = mysql_fetch_assoc ( $res ) ) {
				$data[] = reset ( $row );
			}
	        @mysql_free_result ( $res );                    
	        return $data;
	    }
	        
	    public function _query( $sql, $throw = true ) {
			if ( $this->debug )echo $sql,'<br />';
			@$result = mysql_query ( $sql, $this->dbConnection );             
	        return $result;
	    }  
	    
	
	    public function getAll( $sql ) 
	    {
	    	$res = $this->_query( $sql );
			$data = array ();
			while ( @$row = mysql_fetch_assoc ( $res ) ) {
				$data[] = $row;
			}
			@mysql_free_result ( $res );                  
	        return $data;
	    }
	    
	    public function execute( $sql, $throw = true ) 
	    {
			return $this->_query( $sql, $throw );                    
	    }    

	    public function lastInsertID(){
	    	return mysql_insert_id ();
	    }
	    
	    public function insert( $dbname, $construct ){
			$name = '';
			$value = '';
			foreach( $construct as $key=>$info ){
				$name .= "`$key`,";
				$value .= "'$info',";
			}
			$name  = substr ( $name, 0, -1 );
			$value = substr ( $value, 0, -1 );
			return $this->execute( "insert into `$dbname`($name) values($value)" );
	    }
	    public function update( $dbname, $construct, $where ){
	    	$value = '';
			foreach( $construct as $key=>$info ){
				$value .= "`$key`='$info',";
			}
			$value = substr ( $value, 0, -1 );
			return $this->execute( "update `$dbname` set $value $where" );
	    }
	    public function insert_many( $dbname, $construct_array ){
			$name = '';
			foreach( $construct_array[0] as $key=>$info ){
				$name .= "`$key`,";
			}
			$name  = substr ( $name, 0, -1 );
	    	
			$value = '';
			foreach( $construct_array as $index=>$construct ){
				$buf = '(';
				foreach( $construct as $key=>$info ){
					$buf .= "'$info',";
				}
				$buf = substr ( $buf, 0, -1 );
				$value .= $buf.'),';
			}
			$value = substr( $value, 0, -1 );
			return $this->execute( "insert into `$dbname`($name) values$value" );
	    }
	    function debug(){
    		$this->debug = 1;
    	}
       
        private $debug = 0;
        private $log = array();        
    
        private static $dbConnection;
      
        private $dbHost;
     
        private $dbUser;
            
        private $dbpass;
             
        private $dbName;
             
          
        private $dbCharset ="utf8";
}
        
?>